﻿using System;
using System.Data;
using System.Data.SQLite;
using System.Text;

using Stock.DBUtility;
using Stock.Model;

namespace Stock.Dal
{
    /// <summary>
    /// 数据访问类:StockOptDetailDal
    /// </summary>
    public partial class StockOptDetailDal
    {
        public StockOptDetailDal()
        { }

        #region Method

        /// <summary>
        /// 增加一条数据
        /// </summary>
        public int Add(StockOptDetailEntity model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("insert into StockOptDetail(");
            strSql.Append("S_Code,Opt_Date,Opt_Copies,Opt_Price,Opt_type,Opt_Sum,Opt_Fee,Opt_Profit,Opt_Convertable,Opt_Bonus,Opt_Allotment,User_Code,Opt_Remark)");
            strSql.Append(" values (");
            strSql.Append("@S_Code,@Opt_Date,@Opt_Copies,@Opt_Price,@Opt_type,@Opt_Sum,@Opt_Fee,@Opt_Profit,@Opt_Convertable,@Opt_Bonus,@Opt_Allotment,@User_Code,@Opt_Remark)");
            strSql.Append(";select LAST_INSERT_ROWID()");
            SQLiteParameter[] parameters = {
					new SQLiteParameter("@S_Code", DbType.Int32,8),
					new SQLiteParameter("@Opt_Date", DbType.String),
					new SQLiteParameter("@Opt_Copies", DbType.Int32,8),
					new SQLiteParameter("@Opt_Price", DbType.Decimal,4),
					new SQLiteParameter("@Opt_type", DbType.String),
					new SQLiteParameter("@Opt_Sum", DbType.Decimal,4),
					new SQLiteParameter("@Opt_Fee", DbType.Decimal,4),
					new SQLiteParameter("@Opt_Profit", DbType.Decimal,4),
					new SQLiteParameter("@Opt_Convertable", DbType.Decimal,4),
					new SQLiteParameter("@Opt_Bonus", DbType.Decimal,4),
					new SQLiteParameter("@Opt_Allotment", DbType.Decimal,4),
					new SQLiteParameter("@User_Code", DbType.String),
					new SQLiteParameter("@Opt_Remark", DbType.String)};
            parameters[0].Value = model.S_Code;
            parameters[1].Value = model.Opt_Date;
            parameters[2].Value = model.Opt_Copies;
            parameters[3].Value = model.Opt_Price;
            parameters[4].Value = model.Opt_type;
            parameters[5].Value = model.Opt_Sum;
            parameters[6].Value = model.Opt_Fee;
            parameters[7].Value = model.Opt_Profit;
            parameters[8].Value = model.Opt_Convertable;
            parameters[9].Value = model.Opt_Bonus;
            parameters[10].Value = model.Opt_Allotment;
            parameters[11].Value = model.User_Code;
            parameters[12].Value = model.Opt_Remark;

            object obj = DbHelperSQLite.GetSingle(strSql.ToString(), parameters);
            if (obj == null)
            {
                return 0;
            }
            else
            {
                return Convert.ToInt32(obj);
            }
        }

        /// <summary>
        /// 更新一条数据
        /// </summary>
        public bool Update(StockOptDetailEntity model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("update StockOptDetail set ");
            strSql.Append("S_Code=@S_Code,");
            strSql.Append("Opt_Date=@Opt_Date,");
            strSql.Append("Opt_Copies=@Opt_Copies,");
            strSql.Append("Opt_Price=@Opt_Price,");
            strSql.Append("Opt_type=@Opt_type,");
            strSql.Append("Opt_Sum=@Opt_Sum,");
            strSql.Append("Opt_Fee=@Opt_Fee,");
            strSql.Append("Opt_Profit=@Opt_Profit,");
            strSql.Append("Opt_Convertable=@Opt_Convertable,");
            strSql.Append("Opt_Bonus=@Opt_Bonus,");
            strSql.Append("Opt_Allotment=@Opt_Allotment,");
            strSql.Append("User_Code=@User_Code,");
            strSql.Append("Opt_Remark=@Opt_Remark");
            strSql.Append(" where Opt_Code=@Opt_Code");
            SQLiteParameter[] parameters = {
					new SQLiteParameter("@S_Code", DbType.Int32,8),
					new SQLiteParameter("@Opt_Date", DbType.String),
					new SQLiteParameter("@Opt_Copies", DbType.Int32,8),
					new SQLiteParameter("@Opt_Price", DbType.Decimal,4),
					new SQLiteParameter("@Opt_type", DbType.String),
					new SQLiteParameter("@Opt_Sum", DbType.Decimal,4),
					new SQLiteParameter("@Opt_Fee", DbType.Decimal,4),
					new SQLiteParameter("@Opt_Profit", DbType.Decimal,4),
					new SQLiteParameter("@Opt_Convertable", DbType.Decimal,4),
					new SQLiteParameter("@Opt_Bonus", DbType.Decimal,4),
					new SQLiteParameter("@Opt_Allotment", DbType.Decimal,4),
					new SQLiteParameter("@User_Code", DbType.String),
					new SQLiteParameter("@Opt_Remark", DbType.String),
					new SQLiteParameter("@Opt_Code", DbType.Int32,8)};
            parameters[0].Value = model.S_Code;
            parameters[1].Value = model.Opt_Date;
            parameters[2].Value = model.Opt_Copies;
            parameters[3].Value = model.Opt_Price;
            parameters[4].Value = model.Opt_type;
            parameters[5].Value = model.Opt_Sum;
            parameters[6].Value = model.Opt_Fee;
            parameters[7].Value = model.Opt_Profit;
            parameters[8].Value = model.Opt_Convertable;
            parameters[9].Value = model.Opt_Bonus;
            parameters[10].Value = model.Opt_Allotment;
            parameters[11].Value = model.User_Code;
            parameters[12].Value = model.Opt_Remark;
            parameters[13].Value = model.Opt_Code;

            int rows = DbHelperSQLite.ExecuteSql(strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public StockOptDetailEntity GetModel(int Opt_Code)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select Opt_Code,S_Code,Opt_Date,Opt_Copies,Opt_Price,Opt_type,Opt_Sum,Opt_Fee,Opt_Profit,Opt_Convertable,Opt_Bonus,Opt_Allotment,User_Code,Opt_Remark from StockOptDetail ");
            strSql.Append(" where Opt_Code=@Opt_Code");
            SQLiteParameter[] parameters = {
					new SQLiteParameter("@Opt_Code", DbType.Int32,4)
};
            parameters[0].Value = Opt_Code;

            StockOptDetailEntity model = new StockOptDetailEntity();
            DataSet ds = DbHelperSQLite.Query(strSql.ToString(), parameters);
            if (ds.Tables[0].Rows.Count > 0)
            {
                if (ds.Tables[0].Rows[0]["Opt_Code"] != null && ds.Tables[0].Rows[0]["Opt_Code"].ToString() != "")
                {
                    model.Opt_Code = int.Parse(ds.Tables[0].Rows[0]["Opt_Code"].ToString());
                }
                if (ds.Tables[0].Rows[0]["S_Code"] != null && ds.Tables[0].Rows[0]["S_Code"].ToString() != "")
                {
                    model.S_Code = ds.Tables[0].Rows[0]["S_Code"].ToString();
                }
                if (ds.Tables[0].Rows[0]["Opt_Date"] != null && ds.Tables[0].Rows[0]["Opt_Date"].ToString() != "")
                {
                    model.Opt_Date = ds.Tables[0].Rows[0]["Opt_Date"].ToString();
                }
                if (ds.Tables[0].Rows[0]["Opt_Copies"] != null && ds.Tables[0].Rows[0]["Opt_Copies"].ToString() != "")
                {
                    model.Opt_Copies = int.Parse(ds.Tables[0].Rows[0]["Opt_Copies"].ToString());
                }
                if (ds.Tables[0].Rows[0]["Opt_Price"] != null && ds.Tables[0].Rows[0]["Opt_Price"].ToString() != "")
                {
                    model.Opt_Price = decimal.Parse(ds.Tables[0].Rows[0]["Opt_Price"].ToString());
                }
                if (ds.Tables[0].Rows[0]["Opt_type"] != null && ds.Tables[0].Rows[0]["Opt_type"].ToString() != "")
                {
                    model.Opt_type = ds.Tables[0].Rows[0]["Opt_type"].ToString();
                }
                if (ds.Tables[0].Rows[0]["Opt_Sum"] != null && ds.Tables[0].Rows[0]["Opt_Sum"].ToString() != "")
                {
                    model.Opt_Sum = decimal.Parse(ds.Tables[0].Rows[0]["Opt_Sum"].ToString());
                }
                if (ds.Tables[0].Rows[0]["Opt_Fee"] != null && ds.Tables[0].Rows[0]["Opt_Fee"].ToString() != "")
                {
                    model.Opt_Fee = decimal.Parse(ds.Tables[0].Rows[0]["Opt_Fee"].ToString());
                }
                if (ds.Tables[0].Rows[0]["Opt_Profit"] != null && ds.Tables[0].Rows[0]["Opt_Profit"].ToString() != "")
                {
                    model.Opt_Profit = decimal.Parse(ds.Tables[0].Rows[0]["Opt_Profit"].ToString());
                }
                if (ds.Tables[0].Rows[0]["Opt_Convertable"] != null && ds.Tables[0].Rows[0]["Opt_Convertable"].ToString() != "")
                {
                    model.Opt_Convertable = decimal.Parse(ds.Tables[0].Rows[0]["Opt_Convertable"].ToString());
                }
                if (ds.Tables[0].Rows[0]["Opt_Bonus"] != null && ds.Tables[0].Rows[0]["Opt_Bonus"].ToString() != "")
                {
                    model.Opt_Bonus = decimal.Parse(ds.Tables[0].Rows[0]["Opt_Bonus"].ToString());
                }
                if (ds.Tables[0].Rows[0]["Opt_Allotment"] != null && ds.Tables[0].Rows[0]["Opt_Allotment"].ToString() != "")
                {
                    model.Opt_Allotment = decimal.Parse(ds.Tables[0].Rows[0]["Opt_Allotment"].ToString());
                }
                if (ds.Tables[0].Rows[0]["User_Code"] != null && ds.Tables[0].Rows[0]["User_Code"].ToString() != "")
                {
                    model.User_Code = ds.Tables[0].Rows[0]["User_Code"].ToString();
                }
                if (ds.Tables[0].Rows[0]["Opt_Remark"] != null && ds.Tables[0].Rows[0]["Opt_Remark"].ToString() != "")
                {
                    model.Opt_Remark = ds.Tables[0].Rows[0]["Opt_Remark"].ToString();
                }
                return model;
            }
            else
            {
                return null;
            }
        }

        /// <summary>
        /// 获得数据列表
        /// </summary>
        public DataSet GetList(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select Opt_Code,S_Code,Opt_Date,Opt_Copies,Opt_Price,Opt_type,Opt_Sum,Opt_Fee,Opt_Profit,Opt_Convertable,Opt_Bonus,Opt_Allotment,User_Code,Opt_Remark ");
            strSql.Append(" FROM StockOptDetail ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            return DbHelperSQLite.Query(strSql.ToString());
        }

        /*
        /// <summary>
        /// 分页获取数据列表
        /// </summary>
        public DataSet GetList(int PageSize,int PageIndex,string strWhere)
        {
            SQLiteParameter[] parameters = {
                    new SQLiteParameter("@tblName", DbType.VarChar, 255),
                    new SQLiteParameter("@fldName", DbType.VarChar, 255),
                    new SQLiteParameter("@PageSize", DbType.Int32),
                    new SQLiteParameter("@PageIndex", DbType.Int32),
                    new SQLiteParameter("@IsReCount", DbType.bit),
                    new SQLiteParameter("@OrderType", DbType.bit),
                    new SQLiteParameter("@strWhere", DbType.VarChar,1000),
                    };
            parameters[0].Value = "StockOptDetail";
            parameters[1].Value = "Opt_Code";
            parameters[2].Value = PageSize;
            parameters[3].Value = PageIndex;
            parameters[4].Value = 0;
            parameters[5].Value = 0;
            parameters[6].Value = strWhere;
            return DbHelperSQLite.RunProcedure("UP_GetRecordByPage",parameters,"ds");
        }*/

        #endregion Method
    }
}